<?php

namespace addons\mental\controller;

use addons\mental\model\Testlog;
use addons\mental\model\Remark as remarkModel;

class Remark extends Base
{
    protected $noNeedLogin = ['get_list'];

    public function add_remark()
    {
        $test_id = $this->request->param('test_id'); //记录id      
        $content = $this->request->param('content');
        if (!$test_id || !$content) {
            $this->error(__('Invalid parameters'));
        }
        $testLogModel = new TestLog();
        $test_log = $testLogModel->where('id', '=', $test_id)->where('user_id', '=', $this->auth->id)->find();
        if (empty($test_log)) {
            $this->error(__('No results were found'));
        }
        //状态未完成
        if ($test_log->status != 'finish') {
            $this->error(__('Operation failed'));
        }
        $remarkModel = new remarkModel();
        //判断是否评价过

        $status = $remarkModel->save([
            'main_id' => $test_log->main_id,
            'user_id' => $this->auth->id,
            'content' => $content,
            'log_id' => $test_id
        ]);
        if ($status) {
            $this->success(__('Operation completed'));
        }
        $this->error(__('Operation failed'));
    }

    /**
     * Undocumented function
     * 公共评论列表
     * @return void
     */
    public function get_list()
    {
        $param = $this->request->param();
        $remarkModel = new remarkModel();
        $list = $remarkModel->tableList($param);
        if (!$list->total()) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $list);
    }

    /**
     * Undocumented function
     * 我的评论列表
     * @return void
     */
    public function my_list()
    {
        $param = $this->request->param();
        $param['user_id'] = $this->auth->id;
        $remarkModel = new remarkModel();
        $list = $remarkModel->tableList($param);
        if (!$list->total()) {
            $this->error(__('No results were found'));
        }
        $this->success(__('Operation completed'), $list);
    }
}
